package com.eb.bi.common;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Element;

import com.eb.bi.common.util.FileProcessor;
import com.eb.bi.common.util.XMLProcessor;

public class PluginConfig {
	private String m_errorDesc;
	private HashMap<String,String> m_params = new HashMap<String,String>();
	public boolean load(String configFilePath){
		
		/// Step 1, Check the valid
		FileProcessor FileProcessor = new FileProcessor();
		if(FileProcessor.getTotalFileInfo(configFilePath) == null){
			m_errorDesc = "config "+FileProcessor.getM_errorDesc();
			return false;
		}
		/// Step 2, Load the config from file
		XMLProcessor XMLProcessor = new XMLProcessor();
		XMLProcessor.DocumentCreator(configFilePath);
		if(XMLProcessor.getRoot() == null){
			this.m_errorDesc = "config file has no root element";
			return false;
		}
		List nodes = XMLProcessor.getRoot().elements("param");
		for (Iterator it = nodes.iterator(); it.hasNext();) { 
			
		    Element elm = (Element) it.next();  
		    String name = elm.attributeValue("name");
		    String value = elm.getText();
		    if(name == null || name.trim().equals("")){
		    	continue;
		    }
		    m_params.put(name, value);
		}      
		return true;
	}
	public String getM_errorDesc() {
		return m_errorDesc;
	}
	
	public void print(){
		System.out.println("==========Config Param List==========" );
		System.out.println(m_params);
	}
	public HashMap<String, String> getM_params() {
		return m_params;
	}
}
